##################################################################
# File : /lua/DSlobbysession.lua
# Description : Sets up an empty session
# Author : Neruz
##################################################################

local Prefs = import('/lua/user/prefs.lua')
local MapUtils = import('/lua/ui/maputil.lua')

local defaultOptions = {
    FogOfWar = 'none',
    NoRushOption = 'Off',
    PrebuiltUnits = 'Off',
    Difficulty = 2,
    DoNotShareUnitCap = true,
    Timeouts = -1,
    GameSpeed = 'normal',
    UnitCap = '1000',
    Victory = 'sandbox',
    CheatsEnabled = 'true',
    CivilianAlliance = 'enemy',
}

function VerifyScenarioConfiguration(scenarioInfo)
    if scenarioInfo == nil then
        error("VerifyScenarioConfiguration - no scenarioInfo")
    end
end

local function GetCommandLineOptions()
    local options = table.copy(defaultOptions)
    return options
end

local function SetupLobbySession(scenario)

    local faction = 1

    VerifyScenarioConfiguration(scenario)

    scenario.Options = GetCommandLineOptions()

    sessionInfo = { }
    sessionInfo.playerName = Prefs.GetFromCurrentProfile('Name') or 'Player'
    sessionInfo.focusArmy = 1
    sessionInfo.createReplay = false
    sessionInfo.scenarioInfo = scenario
    sessionInfo.teamInfo = {}
    sessionInfo.scenarioMods = {}

    local armies = {'ARMY_1'}

    local numColors = table.getn(import('/lua/gameColors.lua').GameColors.PlayerColors)

    for index, name in armies do
        sessionInfo.teamInfo[index] = import('/lua/ui/network/lobbyComm.lua').GetDefaultPlayerOptions(sessionInfo.playerName)
        sessionInfo.teamInfo[index].PlayerName = sessionInfo.playerName

        sessionInfo.teamInfo[index].Faction = faction
        sessionInfo.teamInfo[index].Human = true
        sessionInfo.teamInfo[index].ArmyName = name
        sessionInfo.teamInfo[index].PlayerColor = math.mod(index, numColors)
        sessionInfo.teamInfo[index].ArmyColor = math.mod(index, numColors)


    end

#     local extras = MapUtils.GetExtraArmies(sessionInfo.scenarioInfo)
#     if extras then
#         for k,armyName in extras do
#             local index = table.getn( sessionInfo.teamInfo ) + 1
#             sessionInfo.teamInfo[index] = import('/lua/ui/lobby/lobbyComm.lua').GetDefaultPlayerOptions("civilian")
#             sessionInfo.teamInfo[index].PlayerName = 'civilian'
#             sessionInfo.teamInfo[index].Civilian = true
#             sessionInfo.teamInfo[index].ArmyName = armyName
#             sessionInfo.teamInfo[index].Human = false
#             sessionInfo.teamInfo[index].Hero = nil
#         end
#     end

#     return sessionInfo
    return sessionInfo
end

function StartLobbySession()
    local mapName = '/maps/NSTest/nstest_scenario.lua'

    local scenario = import('/lua/ui/maputil.lua').LoadScenario(mapName)
    if not scenario then
        error("Unable to load map " .. mapName)
    end

    local sessionInfo = SetupLobbySession(scenario)
    LaunchSession(SetupSinglePlayerSession(sessionInfo))
end